package com.ftj.controller;

import com.ftj.pojo.CourseOrder;
import com.ftj.service.CourseOrderService;
import com.ftj.vo.CrudVO;
import com.ftj.vo.DataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 21seu.ftj
 */
@Controller
@RequestMapping("/course")
public class CourseOrderController {

    @Autowired
    private CourseOrderService courseOrderService;

    @RequestMapping("/list")
    public String toCourseListPage() {
        return "courseorder/list";
    }


    @RequestMapping("/listJson")
    @ResponseBody
    /**
     * page 显示的当前页
     * limit 每次显示多少条
     */
    public DataVO<CourseOrder> getListJson(CourseOrder condition, int page, int limit) {
        DataVO<CourseOrder> dataVO = courseOrderService.findDataVO(condition, page, limit);
        return dataVO;
    }


    @RequestMapping("/add")
    public String toCourseAddPage() {
        return "courseorder/add";
    }

    /**
     * 添加订单信息
     *
     * @param courseOrder
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public CrudVO save(CourseOrder courseOrder) {
        System.out.println(courseOrder);
        CrudVO result = null;
        boolean status = courseOrderService.save(courseOrder);
        if (status) {
            result = new CrudVO(1, "添加成功！");
        } else {
            result = new CrudVO(-1, "添加失败！");
        }
        return result;
    }

    /**
     * 查看订单详细信息
     *
     * @param order_id
     * @param model
     * @return
     */
    @RequestMapping("/detail/{order_id}")
    public String toCourseDetailPage(@PathVariable("order_id") String order_id, Model model) {
        CourseOrder courseOrder = courseOrderService.findByOrderId(order_id);
        model.addAttribute("order", courseOrder);
        return "courseorder/detail";
    }

    /**
     * 删除订单
     *
     * @param order_id
     * @return
     */
    @RequestMapping("/del")
    @ResponseBody
    public CrudVO delCourseOrder(@RequestParam("order_id") String order_id) {
        CrudVO crudVO = null;
        System.out.println(order_id);
        boolean result = courseOrderService.delOrderByOrderId(order_id);
        if (result) {
            crudVO = new CrudVO(1, "删除成功！");
        } else {
            crudVO = new CrudVO(-1, "删除失败！");
        }
        return crudVO;
    }

    @RequestMapping("/edit/{order_id}")
    public String toCourseEditPage(@PathVariable("order_id") String order_id, Model model) {
        System.out.println("修改订单");
        CourseOrder courseOrders = courseOrderService.findByOrderId(order_id);
        model.addAttribute("courses", courseOrders);
        return "courseorder/edit";
    }

    @RequestMapping("/update/{order_id}")
    @ResponseBody
    public CrudVO updateCourseOrder(@PathVariable("order_id") String order_id,CourseOrder courseOrder) {
        CrudVO crudVO = null;
        boolean result = courseOrderService.updateOrderById(order_id, courseOrder);
        if (result){
            crudVO = new CrudVO(1,"数据更新成功");
        }else {
            crudVO = new CrudVO(-1,"数据更新发生错误");
        }
        return crudVO;
    }
}
